﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using ADI.Security.Membership;

namespace ADI.Security.Controls
{
    public class RoleGroupCollection : Collection<RoleGroup> { }

    [ParseChildren(true), PersistChildren(false)]
    public class RoleGroup : ITemplate
    {
        public string ActiveRoles { get; set; }
        public string RequiresManager { get; set; }

        [Browsable(false)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateInstance(TemplateInstance.Single)]
        public ITemplate ContentTemplate { get; set; }

        public void InstantiateIn(Control container)
        {
            bool show = false;
            string[] roles;
            string[] manager;

            if (!string.IsNullOrEmpty(ActiveRoles))
                roles = this.ActiveRoles.Split(',').Select(r => r.Trim().ToLower()).ToArray();
            else
                throw new ArgumentException("Il controllo non può non avere ruoli definiti se il tag RoleGroup è presente.");

            if (!string.IsNullOrEmpty(RequiresManager))
                manager = this.RequiresManager.Split(',').Select(r => r.Trim().ToLower()).ToArray();
            else
                manager = new string[] { string.Empty };

            foreach (var r in roles)
            {
                bool isInRole = Roles.IsUserInRole(r);
                bool requiresManager = manager.Contains(r);
                bool isManager = false;
                if (requiresManager)
                    isManager = ((ADISSORoleProvider)(Roles.Provider)).IsUserManager(HttpContext.Current.User.Identity.Name, r);

                if (isInRole)
                {
                    if (requiresManager && isManager == false) continue;
                    show = true;
                    break;
                }
            }

            if (show)
                ContentTemplate.InstantiateIn(container);
        }
    }

    [ParseChildren(true), PersistChildren(false)]
    public class ADLoginView : WebControl, INamingContainer
    {
        [Browsable(false)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateInstance(TemplateInstance.Single)]
        public ITemplate AnonymousTemplate { get; set; }

        [Browsable(false)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateInstance(TemplateInstance.Single)]
        public ITemplate LoggedInTemplate { get; set; }

        [Browsable(false)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public RoleGroupCollection RoleGroups { get; set; }

        protected override void CreateChildControls()
        {
            Controls.Clear();

            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                if (RoleGroups != null)
                {
                    foreach (var item in RoleGroups)
                    {
                        item.InstantiateIn(this);
                    }
                }
                else
                {
                    if (LoggedInTemplate != null)
                        LoggedInTemplate.InstantiateIn(this);
                }
            }
            else
            {
                if (AnonymousTemplate != null)
                    AnonymousTemplate.InstantiateIn(this);
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            RenderContents(writer);
        }
    }
}